


import baseItemScript from "../../base/baseItemScript";
import gameModel from "./gameModel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class bsaeTriangle extends baseItemScript {
    private outsidePoint:any;
    private outsidePoint2:any;
    private SeflCollision:any;
    private outsideNodePoint:any=[];
    private aimBall:any;

    @property(cc.Node)
    outsideListClo:cc.Node=null;

    @property(cc.Size)
    CollisionSize:cc.Size=null;

    start(){
        // this.outsidePoint=[];
        this.outsidePoint2=[cc.v2(0,0),cc.v2(0,0)];
        this.outsideNodePoint=[cc.v2(158,-380),cc.v2(-405,53),cc.v2(250,324)]
        this.aimBall=this._get_("aimball")
        
    }

    //创建碰撞点并进行碰撞检测
    creatorCollisionPoint(){
        if(this.outsideListClo.children.length==0||gameModel.getInstance().isGameOver){
            return;
        }
        var pointA=cc.v2(this.aimBall.x-this.CollisionSize.width/2,this.aimBall.y+this.CollisionSize.height/2);
        var pointC=cc.v2(this.aimBall.x+this.CollisionSize.width/2,this.aimBall.y-this.CollisionSize.height/2);
        this.SeflCollision=[pointA,pointC];
        this.outsideListClo.children.sort(function(a,b){return a.scale-b.scale});
        // console.error(this.SeflCollision);
        // console.error( this.outsideListClo.children[0].rotation )
        switch(this.outsideListClo.children[0].rotation){
            case 0:this.outsidePoint=[this.outsideNodePoint[0],this.outsideNodePoint[2]];
                break;
            case 120:this.outsidePoint=[this.outsideNodePoint[0],this.outsideNodePoint[1]];
                break;
            case 240:this.outsidePoint=[this.outsideNodePoint[1],this.outsideNodePoint[2]];
                break;
        }
        // console.log("x,y ",this.outsidePoint)
        this.outsidePoint2[0].x=this.outsidePoint[0].x*this.outsideListClo.children[0].scale;
        this.outsidePoint2[0].y=this.outsidePoint[0].y*this.outsideListClo.children[0].scale;
        this.outsidePoint2[1].x=this.outsidePoint[1].x*this.outsideListClo.children[0].scale;
        this.outsidePoint2[1].y=this.outsidePoint[1].y*this.outsideListClo.children[0].scale;
        // console.error( this.outsidePoint2[0].x,this.outsidePoint2[0].y )

        for(var i=0;i<2;i++){
            
            if(this.outsidePoint2[i].x>=this.SeflCollision[0].x&&this.outsidePoint2[i].x<=this.SeflCollision[1].x&&
                this.outsidePoint2[i].y<=this.SeflCollision[0].y&&this.outsidePoint2[i].y>=this.SeflCollision[1].y
            ){
                if(!gameModel.getInstance().isGameOver){
                    console.log("发生碰撞");
                    (<any>window).CustomEventManager.trigger({type:"gameState",content:{state:"over"}})
                    gameModel.getInstance().isGameOver=true;
                }
                
            }
            
        }
        
    }
    

    // called every frame, uncomment this function to activate update callback
    update(dt) {
        this.creatorCollisionPoint();
    }
}
